﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using ProjectBase.Utils;

namespace CMS.Web
{
    public class BaseUserControl : System.Web.UI.UserControl
    {
        protected static string baseUrl;
        protected static string baseUrlWithOnlyID;
        protected static string BaseUrlWithRoot;
        protected static string fName;
        /// <summary>
        /// 
        /// </summary>
        public BaseUserControl()
        {
            fName = HttpContext.Current.Request.QueryString.Get("fName");
            string tabID = HttpContext.Current.Request.QueryString.Get("tabID");
            if (tabID == null) tabID = "";
            if ("".Equals(tabID))
                tabID = "1";
            baseUrl = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + HttpContext.Current.Request.ApplicationPath;
            baseUrl += "/";
            BaseUrlWithRoot = baseUrl;
            baseUrlWithOnlyID = baseUrl;
            string page = HttpContext.Current.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1];
            baseUrl += page + "?" + "tabID=" + tabID + "&" + "fName=" + fName;
            BaseUrlWithRoot += page;
            baseUrlWithOnlyID = page + "?" + "tabID=" + tabID;
            String year = HttpContext.Current.Request.QueryString.Get("y");
            if (year != null && year != "")
            {
                baseUrl += "&y=" + year;
                BaseUrlWithRoot += "?y=" + year;
                baseUrlWithOnlyID += "&y=" + year;
            }

        }
        public Int32 FName
        {
            get
            {
                return CUtilities.IntegerForNull(fName);
            }
        }
        public string BaseUrl
        {
            get
            {
                return baseUrl;
            }
        }

        public string BaseUrlWithOnlyID
        {
            get
            {
                return baseUrlWithOnlyID;
            }
        }

        /**
         * @dvchinh
         * Hien thi trang thai cho cac danh muc
         */
        public string ShowStatus(bool status)
        {
            return (status) ? "Sử dụng" : "Không sử dụng";
        }

        /**
         * @dvchinh
         * Hien thi trang thai cho cac danh muc
         */
        public string ShowCompdenuStatus(int status)
        {
            string res = "";
            switch (status)
            {
                case 1:
                    res = "Chờ xử lý";
                    break;
                case 2:
                    res = "Đang xử lý";
                    break;
                case 3:
                    res = "Kết thúc";
                    break;
                default:
                    res = "Chờ xử lý";
                    break;
            }
            return res;
        }

        /**
         * @dvchinh
         * Hien thi trang thai cho giao viec
         */
        public string ShowStatusAssignTask(int status, DateTime finishdate, DateTime realfinishdate)
        {
            string res = "";
            // Qua han
            String date1 = realfinishdate.ToString("yyyyMMdd");
            String date2 = finishdate.ToString("yyyyMMdd");
            switch (status)
            {
                case 1:
                    date1 = DateTime.Now.ToString("yyyyMMdd");
                    if (date1.CompareTo(date2) > 0)
                    {
                        res = "Chưa xử lý (quá hạn)";
                    }
                    else
                    {
                        res = "Chưa xử lý";
                    }
                    break;
                case 2:
                    date1 = DateTime.Now.ToString("yyyyMMdd");
                    if (date1.CompareTo(date2) > 0)
                    {
                        res = "Đang xử lý (quá hạn)";
                    }
                    else
                    {
                        res = "Đang xử lý";
                    }
                    break;
                case 3:
                    if (date1.CompareTo(date2) > 0)
                    {
                        res = "Đã xử lý (quá hạn)";
                    }
                    else
                    {
                        res = "Đã xử lý";
                    }
                    break;
                default:
                    if (date1.CompareTo(date2) > 0)
                    {
                        res = "Chưa chuyển (quá hạn)";
                    }
                    else
                    {
                        res = "Chưa chuyển";
                    }
                    break;
            }


            return res;
        }

        /**
         * @dvchinh
         * Hien thi phan loai
         */
        public string ShowClassify(bool status)
        {
            return (status) ? "Sổ văn bản đi" : "Sổ văn bản đến";
        }

        /**
         * @dvchinh
         * Lay link xem chi tiet (van ban den)
         */
        public string getLink()
        {
            return this.BaseUrlWithOnlyID + "&fName=IDDetail";
        }

        /**
         * @nvkhoa
         * added 16/4/09
         * Lay link xem chi tiet (van ban den noi bo)
         */
        public string getLinkComeDocInter()
        {
            return this.BaseUrlWithOnlyID + "&fName=IIDDetail";
        }

        /**
         * @nvkhoa
         * added 16/4/09
         * Lay link xem chi tiet (van ban di noi bo)
         */
        public string getLinkOutDocInter()
        {
            return this.BaseUrlWithOnlyID + "&fName=IODDetail";
        }

        /**
         * @dvchinh
         * Lay link xem chi tiet (van ban di)
         */
        public string getLinkOutDetail()
        {
            return this.BaseUrlWithOnlyID + "&fName=ODDetail";
        }

        /**
         * @dvchinh
         * Lay link xem chi tiet (giao viec)
         */
        public string getLinkTaskDetail()
        {
            return this.BaseUrlWithOnlyID + "&fName=TaskDetail";
        }

        public string getLinkFileDinhKem(string DinhKem)
        {
            if (DinhKem.Equals("True"))
            {
                return "<a href='" + this.BaseUrlWithOnlyID + "&fName=IDDetail'><img alt='' src='Images/Icon_attach.gif' style='border: 0;'/></a>";
            }
            else
            {
                return "";
            }
        }

        public string getLinkFileDinhKem(string DinhKem, string id)
        {
            if (DinhKem.Equals("True"))
            {
                return "<a href='" + this.BaseUrlWithOnlyID + "&fName=IDDetail&Id=" + id + "'><img alt='' src='Images/Icon_attach.gif' style='border: 0;'/></a>";
            }
            else
            {
                return "";
            }
        }

        /*
        * nvkhoa added 10/4/09
        * */
        public string getLinkFileDinhKemForOutDoc(string DinhKem, string id)
        {
            if (DinhKem.Equals("True"))
            {
                return "<a href='" + this.BaseUrlWithOnlyID + "&fName=ODDetail&Id=" + id + "'><img alt='' src='Images/Icon_attach.gif' style='border: 0;'/></a>";
            }
            else
            {
                return "";
            }
        }
        /*
        * nvkhoa added 16/4/09
        * */
        public string getLinkAttachOutInter(string DinhKem, string id)
        {
            if (DinhKem.Equals("True"))
            {
                return "<a href='" + this.BaseUrlWithOnlyID + "&fName=IODDetail&Id=" + id + "'><img alt='' src='Images/Icon_attach.gif' style='border: 0;'/></a>";
            }
            else
            {
                return "";
            }
        }
        /*
        * nvkhoa added 16/4/09
        * */
        public string getLinkAttachComeInter(string DinhKem, string id)
        {
            if (DinhKem.Equals("True"))
            {
                return "<a href='" + this.BaseUrlWithOnlyID + "&fName=IIDDetail&Id=" + id + "'><img alt='' src='Images/Icon_attach.gif' style='border: 0;'/></a>";
            }
            else
            {
                return "";
            }
        }

        public string FullBaseUrl
        {
            get
            {
                string url = SiteNavigation.GetRootPath();
                if (!url.EndsWith("/"))
                {
                    url += "/";
                }
                return url;
            }
        }

        public CMS.Core.Domain.NguoiSuDungInfo GetNSD()
        {
            return ((CMS.Web.Site)this.Page.Master).userInfo;

        }
        public bool IsHuyDanhMuc()
        {
            CMS.Core.Domain.NguoiSuDungInfo nsdInfo = this.GetNSD();
            if (nsdInfo != null)
            {
                return CMS.Data.KiemTraQuyenDao.Instance.CheckQuyenHuyDanhMuc(nsdInfo.ID);
            }
            return false;
        }
        public bool IsHuyCongTT()
        {
            CMS.Core.Domain.NguoiSuDungInfo nsdInfo = this.GetNSD();
            if (nsdInfo != null)
            {
                return CMS.Data.KiemTraQuyenDao.Instance.CheckQuyenHuyCongTT(nsdInfo.ID);
            }
            return false;
        }
        public bool IsHuyDonThu()
        {
            CMS.Core.Domain.NguoiSuDungInfo nsdInfo = this.GetNSD();
            if (nsdInfo != null)
            {
                return CMS.Data.KiemTraQuyenDao.Instance.CheckQuyenHuyDonThu(nsdInfo.ID);
            }
            return false;
        }
        public bool IsHuyTiepDan()
        {
            CMS.Core.Domain.NguoiSuDungInfo nsdInfo = this.GetNSD();
            if (nsdInfo != null)
            {
                return CMS.Data.KiemTraQuyenDao.Instance.CheckQuyenHuyTiepDan(nsdInfo.ID);
            }
            return false;
        }
    }
}

